package com.koron.css2.systemManage.servlet;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.bean.UserRangeBean;
import com.koron.css2.systemManage.dto.OrgUserAddDto;
import com.koron.css2.systemManage.mapper.CommlibMapper;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.css2.systemManage.service.IDataRangeService;
import com.koron.css2.systemManage.service.ServiceFactory;
import com.koron.util.Constant;
import com.koron.util.DBSourceUtils;
import com.koron.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class OrgUserAdd implements ServerInterface {
	
	private static Logger log = LogManager.getLogger(OrgUserAdd.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<Void> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", Void.class);
		try {
			OrgUserAddDto account = JsonUtils.objectToPojo(req.getData(), OrgUserAddDto.class);
			if(StringUtils.isEmpty(account.getCompanyNo())) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("组织不能为空");
				return msg;
			}
			if(account.getAccount() == null || account.getAccount().size() == 0) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("人员不能为空");
				return msg;
			}
			if(account.getAccount().size() > 500) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("一次性添加人员不能超过500");
				return msg;
			}
			OrgUserMapper mapper = factory.getMapper(OrgUserMapper.class, DBSourceUtils.getDbEnv(account.getCompanyNo()));
			CommlibMapper cmapper = factory.getMapper(CommlibMapper.class);
			List<OrgUserBean> list = cmapper.selectByAccount(account.getAccount());
			IDataRangeService dataRangeService = ServiceFactory.getDataRangeService();
			for (OrgUserBean pub : list) {
				OrgUserBean user = mapper.selectByAccount(pub.getAccount());
				if(user == null) {
					pub.setCreateInfo(userInfo);
					int iCount = mapper.insert(pub);
					if(iCount > 0) {
						List<UserRangeBean> ranges = dataRangeService.getUserRangeValueList(pub.getCode(), ApplicationConfig.getAppCustomerAppid(), "company");
						List<String> companyList = ranges.stream().map(t -> t.getValue()).collect(Collectors.toList());
						boolean b = companyList.stream().anyMatch(task -> userInfo.getCurWaterCode().equals(task));
						if(b == false) {
							dataRangeService.addUserRangeValue(ApplicationConfig.getAppCustomerAppid(), pub.getCode(), "company", Arrays.asList(userInfo.getCurWaterCode()));
						}
					}
					log.info("已添加用户：{}", pub.getUserName());
				}
			}
		} catch (Exception e) {
			log.error("添加用户出错：" + e);
			msg.setCode(Constant.MESSAGE_INT_FAIL);
			msg.setDescription("添加用户出错");
		}
		return msg;
	}

}
